package com.pmcc.core.config.utils;

import org.springframework.data.domain.AbstractPageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.io.Serializable;

/**
 * Created by syk on 17/7/3.
 */
public abstract class AntAbstractPageRequest implements Pageable,Serializable {

    private static final long serialVersionUID = 1232825578694716871L;

    private  int page;
    private  int pageSize;

    /**
     * Creates a new {@link AntAbstractPageRequest}. Pages are zero indexed, thus providing 0 for {@code page} will return
     * the first page.
     *
     * @param page must not be less than zero.
     * @param pageSize must not be less than one.
     */
    public AntAbstractPageRequest(int page, int pageSize) {

        if (page < 0) {
            throw new IllegalArgumentException("Page index must not be less than zero!");
        }

        if (pageSize < 1) {
            throw new IllegalArgumentException("Page size must not be less than one!");
        }

        this.page = page;
        this.pageSize = pageSize;
    }


    public int getPage() {
        return page;
    }



    /*
         * (non-Javadoc)
         * @see org.springframework.data.domain.Pageable#getPageSize()
         */
    public int getPageSize() {
        return pageSize;
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Pageable#getPageNumber()
     */
    public int getPageNumber() {
        return page-1;
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Pageable#getOffset()
     */
    public int getOffset() {
        return (page-1) * pageSize;
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Pageable#hasPrevious()
     */
    public boolean hasPrevious() {
        return page-1 > 0;
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Pageable#previousOrFirst()
     */
    public Pageable previousOrFirst() {
        return hasPrevious() ? previous() : first();
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Pageable#next()
     */
    public abstract Pageable next();

    /**
     * Returns the {@link Pageable} requesting the previous {@link Page}.
     *
     * @return
     */
    public abstract Pageable previous();

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Pageable#first()
     */
    public abstract Pageable first();

    /*
     * (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {

        final int prime = 31;
        int result = 1;

        result = prime * result + page;
        result = prime * result + pageSize;

        return result;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {

        if (this == obj) {
            return true;
        }

        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }

        AntAbstractPageRequest other = (AntAbstractPageRequest) obj;
        return this.page == other.pageSize && this.pageSize == other.pageSize;
    }

    public void setPage(int page) {
        this.page=page;
    }



    /*
         * (non-Javadoc)
         * @see org.springframework.data.domain.Pageable#getPageSize()
         */
    public void setPageSize(int pageSize) {
        this.pageSize=pageSize;
    }


}
